import {
  http
} from '../../../utils/request'

var api = {};
var util = {};
var user = {};

var app = getApp();
Page({
  data: {
    username: '',
    password: '',
    userType: 0,
    // username: '15162139069',
    // password: '234567',
    demoCheckboxMax: false,
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成

  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  onChange() {
    this.setData({
      demoCheckboxMax: !this.data.demoCheckboxMax
    })
  },
  accountRegistry() {
    wx.navigateTo({
      url: '/pages/auth/register/register',
    })
  },
  async accountLogin() {
    var that = this;

    if (this.data.password.length < 1 || this.data.username.length < 1) {
      wx.showModal({
        title: '错误信息',
        content: '请输入用户名和密码',
        showCancel: false
      });
      return false;
    }

    if (!this.data.demoCheckboxMax) {
      wx.showModal({
        title: '错误信息',
        content: '请先勾选服务协议',
        showCancel: false
      });
      return false;
    }

    const userInfo = await http('POST', '/login', {
      mobile: this.data.username,
      password: this.data.password,
      userType: this.data.userType,
    })
    if (userInfo) {
      app.globalData.hasLogin = true;
      wx.setStorageSync('userInfo', userInfo);
      wx.switchTab({
        url: '/pages/home/home'
      });
    }
  },
  bindUsernameInput: function (e) {

    this.setData({
      username: e.detail.value
    });
  },
  bindPasswordInput: function (e) {

    this.setData({
      password: e.detail.value
    });
  },
  bindCodeInput: function (e) {

    this.setData({
      code: e.detail.value
    });
  },
  clearInput: function (e) {
    switch (e.currentTarget.id) {
      case 'clear-username':
        this.setData({
          username: ''
        });
        break;
      case 'clear-password':
        this.setData({
          password: ''
        });
        break;
      case 'clear-code':
        this.setData({
          code: ''
        });
        break;
    }
  },
  radioChange(e) {
    this.setData({
      userType: e.detail.value
    })
  },
})